<template>
  <el-container class="examAddForm-box">
    <el-header :style="checkmode ? 'height: 100%' : 'height: 25px;'">
      <el-row :gutter="10">
        <el-col :span="6">
          <span @click="() => {this.$router.go(-1)}" class="font-blue my-btn">返回上级</span>

        </el-col>
        <el-col :push="3" :span="5">
          <span v-if="!checkmode">{{title}}</span>
          <el-tabs v-else style="display: flex;" v-model="activeName" @tab-click="tabClick">
            <el-tab-pane :label="title+'详情'" name="1"></el-tab-pane>
            <el-tab-pane label="成员列表" name="2"></el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>

    </el-header>
    <el-main>
      <!-- 查看模式下的成员列表 S -->
      <template v-if="checkmode">

          <!-- 筛选部分 S -->
          <el-row v-show="activeName == '2'" :gutter="5">

          	<el-col  :span="2"><el-button><a :href="exportUrlupexam" class="daocu">导出成绩</a></el-button></el-col>
          	<el-col :span="3" :offset="10">
			  			<template>
							  <el-select v-model="value" clearable placeholder="筛选成绩">
							    <el-option
							      v-for="item in options"
							      :key="item.value"
							      :label="item.label"
							      :value="item.value">
							    </el-option>
							  </el-select>
							</template></el-col>
            <el-col  :span="5"><el-input v-model="keyword_input" placeholder="请输入关键词"></el-input></el-col>
            <el-col :span="2"><el-button  @click="keywordSearch" >搜索</el-button></el-col>
          </el-row>
          <!-- 筛选部分 E -->
          <Table
            v-show="activeName == '2'"
            :border="true"
            size="small"
            ref="selectTable"
            maxHeight="470"
            :tableData="tableData"
            :inits="tableinits"
            :operation="operationinits">
			      <template slot-scope="{rowScope}">

						<el-button @click="check(rowScope)" type="text" size="small">查看</el-button>

			      </template>
          </Table>
          <!-- 分页器 S -->
          <el-row v-show="activeName == '2'">
            <el-col>
              <el-pagination
                layout="prev, pager, next"
                :page-size="15"
                :current-page="pageNo"
                :total="total"
                @current-change="pageNoChange">
              </el-pagination>
            </el-col>
          </el-row>
          <!-- 分页器 E -->

      </template>

 <!-- 查看试卷详情的弹窗 S -->
    <el-dialog
      title="试卷详情"
      :visible.sync="dialogVisible"
      width="70%">
      <Table
        maxHeight="300"
        :tableData="detail_tableData"
        :inits="detail_tableinits"
        :operation="operationinit">
				      <template slot-scope="{rowScope}">
				        <el-button @click="toError(rowScope)" type="text" size="small">查看</el-button>
				      </template>
      </Table>
      <!-- 分页器 S -->
      <el-row>
        <el-col>
          <el-pagination
            layout="prev, pager, next"
            :page-size="15"
            :current-page="detail_pageNo"
            :total="detail_total"
            @current-change="detail_pageNoChange">
          </el-pagination>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
      </el-dialog>



      <!-- 查看模式下的成员列表 E -->
      <el-form
        ref="addform"
        :model="formData"
        label-width="100px"
        size="mini"
        :disabled="checkmode"
        v-show="!checkmode ? true : activeName == '1' ? true : false"
        >

        <el-form-item label="考试名称：" prop="name" :rules='{required:!checkmode,message:"该项必填"}'>
         <!-- <el-select
            v-model="formData.name"
            filterable
            allow-create>
            <el-option
              v-for="item in nameOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select> -->
					<el-autocomplete
					v-model='formData.name'
					placeholder='请输入考试名称'
					 :fetch-suggestions="querySearch"
					 value-key='name'
					>

					</el-autocomplete>
        </el-form-item>

        <el-form-item label="调用试卷：" prop="exam_id" :rules='{required:!checkmode,message:"该项必填"}'>
          <el-select v-model="formData.exam_id" placeholder="请选择分类">
            <el-option v-for="item in testOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item class="my-textarea" label="考试说明：" prop="explain" :rules='{required:!checkmode,message:"该项必填"}'>
          <el-input v-model="formData.explain" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>

        <el-form-item label="时间：" prop="date" :rules='{required:!checkmode,message:"该项必填"}'>
          <el-date-picker
            v-model="formData.date"
            type="datetimerange"
            range-separator="至"
						value-format='yyyy-MM-dd HH:mm:ss'
            start-placeholder="开始日期"
            end-placeholder="结束日期">
            </el-date-picker>
        </el-form-item>

        <el-form-item class="times-input" label="限时：" prop="limitTime" :rules='{required:!checkmode,message:"该项必填"}'>
          <el-input v-model="formData.limitTime" placeholder="请输入时间"></el-input>
        </el-form-item>

      <!--  <el-form-item class="times-input"  label="考试次数：" prop="examTimes">
          <el-input v-model="formData.examTimes" placeholder="请输入次数"></el-input>
        </el-form-item> -->

        <el-form-item class="times-input"  label="通过分数：" prop="passScore" :rules='{required:!checkmode,message:"该项必填"}'>
          <el-input v-model="formData.passScore" placeholder="请输入分数"></el-input>
        </el-form-item>

        <template v-if="title == '考试'">
          <el-form-item class="times-input"  label="考试次数：" prop="testTimes" :rules='{required:!checkmode,message:"该项必填"}'>
            <el-input-number v-model="formData.testTimes" :min="1" ></el-input-number>
          </el-form-item>

          <el-form-item class="times-input"  label="补考次数：" prop="makeupTimes" :rules='{required:!checkmode,message:"该项必填"}'>
            <el-input-number v-model="formData.makeupTimes" :min="0" ></el-input-number>
          </el-form-item>
        </template>

        <!-- <el-form-item class="times-input"  label="补考次数：" prop="muTimes">
          <el-input v-model="formData.muTimes" placeholder="请输入次数"></el-input>
        </el-form-item> -->

       <!-- <el-form-item label="交卷后：" prop="afterSubmit">
          <el-checkbox-group
            v-model="formData.afterSubmit">
            <el-checkbox v-for="status in afterStatus" :label="status" :key="status">{{status}}</el-checkbox></el-checkbox-group>
        </el-form-item>

        <el-form-item label="交卷后：" prop="afterExam">
          <el-checkbox-group
            v-model="formData.afterExam">
            <el-checkbox v-for="status in afterStatus" :label="status" :key="status">{{status}}</el-checkbox></el-checkbox-group>
        </el-form-item> -->

        <!-- <el-form-item label="考试提醒：" prop="remind">
          <el-checkbox v-model="formData.remind"></el-checkbox>
        </el-form-item> -->

        <el-form-item label="培训对象：" prop="target">
          <el-row :gutter="10" v-if="!checkmode">
            <el-col :span="3">
              <el-button style="width:100%;" type="primary" @click="addGroup">+ 添加组织机构</el-button>
            </el-col>
            <el-col :span="3">
              <el-button style="width:100%;" type="primary" @click="role_page(1,true)">+ 添加工作角色</el-button>
            </el-col>
            <el-col :span="3">
              <el-button style="width:100%;" type="primary" @click="addPerson(1,true)">+ 添加个人</el-button>
            </el-col>
            <el-col :span="3">
              <el-button style="width:100%;" type="primary" @click="addVisitor()">+ 添加访客</el-button>
            </el-col>
            <el-col :span="3">
              <el-button style="width:100%;" type="primary" @click="addVendor()">+ 添加承包商</el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-table
              :data="formData.assignment"
              border
              style="width: 80%">
              <el-table-column
                prop="typename"
                label="类型"
                width="180">
              </el-table-column>
              <el-table-column
                prop="name"
                label="名称">
              </el-table-column>
              <el-table-column
                prop="num"
                label="人数"
                width="180">
              </el-table-column>
              <el-table-column
                v-if="!checkmode"
                fixed="right"
                label="操作"
								align='center'
                width="120">
                <template slot-scope="scope">
                  <!-- <el-button @click="filter(scope)" type="text" size="small">筛选</el-button> -->
                  <el-button @click="del(scope)" type="text" size="small">删除</el-button>
                </template>
              </el-table-column></el-table>
          </el-row>
        </el-form-item>

        <el-form-item v-if="!checkmode">
          <el-button type="primary" @click="save('save')">保存</el-button>
					<el-button type='primary' @click="save('publish')">发布</el-button>
        </el-form-item>

      </el-form>
      <template v-if="!checkmode">
  			<!-- 组织机构 -->
  			<el-dialog :visible='organization' title='请选择组织机构' width='40%' @close='organization=false'>
  				<el-row>
  					<el-input placeholder='请输入组织机构名称' v-model='organizationf_filter'></el-input>
  				</el-row>
  				<el-row>
  					<el-tree
  					ref='organization'
  					:data='organization_form'
  					default-expand-all
  					node-key='id'
  					:filter-node-method='treefilter'>
  						<span slot-scope='{node,data}' class="custom-tree-node">
  							<span>{{data.name}}</span>
  							<span v-if='data._select'>已选</span>
  							<span v-else>
  								<el-button type='text' @click.stop='orga_active(data)'>选择</el-button>
  							</span>
  						</span>
  					</el-tree>
  				</el-row>
  			</el-dialog>

  			<el-dialog :visible='user_show' title='添加个人' width='40%' @close='user_show=false'>
  				 <el-row>
  					 <el-col :span='4'>
  						 <el-button size='small'>显示已选</el-button>
  					 </el-col>
  					 <el-col :span='4'>
  						<el-button size='small' @click='clear_user_select'>清除已选</el-button>
  					 </el-col>
  					 <el-col :span='11' :push='2'>
  						 <el-input placeholder='请输入人员名称进行搜索' size='small' v-model='user_search'></el-input>
  					 </el-col>
  					 <el-col :span='3' :push='2' style='text-align: right;'>
  						 <el-button size='small' @click='user_soso'>搜索</el-button>
  					 </el-col>
  				 </el-row>
  				 <el-row>
  					 <el-table
  					 :data='userdata.data'
  					 size='small'
  					 ref='usertable'
  					 @select='user_active'
  					 @select-all='user_active'>
  						 <el-table-column type='selection' width='55' align='center'></el-table-column>
  						 <el-table-column label='姓名' prop='username'></el-table-column>
  						 <el-table-column label='昵称' prop='name'></el-table-column>
  						 <el-table-column label='电话' prop='mobile'></el-table-column>
  					 </el-table>
  				 </el-row>
  				 <el-row style='margin-top: 10px;text-align: right;'>
  				 	<el-pagination small :total='userdata.total'
  				 	:current-page = 'userdata.page'
  					layout="prev, pager, next"
  					:page-size='15'
  					 @current-change='addPerson'></el-pagination>
  				 </el-row>
  				 <el-row style='margin-top: 10px;text-align: center;'>
  					 <el-button type='primary' @click='submit_user'>确定</el-button>
  				 </el-row>
  			</el-dialog>


			<el-dialog title='添加工作角色' width='60%' @close='role_show=false' :visible='role_show'>
				<el-table :data='role_data.data'
				@select='role_select'
				@select-all='role_select'
				ref='role_from'>
					<el-table-column type='selection' width='55px' align='center'></el-table-column>
					<el-table-column label='角色名称' prop='name' align='center'></el-table-column>
					<el-table-column label='创建时间' prop='created_at' align='center'></el-table-column>
				</el-table>
				<el-row style='text-align: right;margin-top: 10px;'>
					<el-pagination
					:total='role_data.total'
					:current-page='role_data.page'
					:page-size='15'
					@current-change='role_page'></el-pagination>
				</el-row>
				<el-row style='text-align: center;margin-top: 10px;'>
					<el-button type='primary' @click='role_save'>保存</el-button>
				</el-row>
			</el-dialog>

      </template>
    </el-main>
  </el-container>
</template>

<script>
import Table from '@/components/Table'

export default {
  name: 'ExamAddForm',
  components: {
    Table
  },
  data () {
    return {
    	id:'',
      pageNo: 1,
      total: 0,
      keyword_input: '',
      tableData: [],
      tableinits: [
        { label: '姓名', prop: 'username' },
        { label: '考试次数', prop: 'finish_count'},
        { label: '最后考试时间', prop: 'last_submit' },
        { label: '通过', prop: 'is_pass' },
        { label: '成绩', prop: 'score' ,fontType: true, fontBold: true}

      ],
			operationinits: { width: '140', label: '查看试卷' },
      activeName: '1',
			role_show:false,
			role_data:{
				data:[],
				page:0,
				total:0
			},
			role_history:{},
			role_page_index:{},
			organization:false,
			title:"考试",
			organizationf_filter:'',
			organization_form:[],
			user_show:false,
			user_page_data:{},
			user_active_list:{},
			userdata:{
				page:1,
				data:[],
				total:0
			},
			user_search:"",
      formData: {
        name:'',
				exam_id:'',
				explain:'',
				date:'',
				limitTime:'',
				passScore:'',
        assignment: [],
        testTimes: 1,
        makeupTimes: 0
      },
      nameOptions:[],
      testOptions:[],
      afterStatus:[],
      checkmode: false,
      options: [
        {
          value: 'pass',
          label: '通过'
        }, {
          value: 'no-pass',
          label: '未通过'
        }, {
          value: 'no-score',
          label: '没有成绩'
        }
      ],
      value: '',
       dialogVisible: false,
      detail_tableData: [],
      detail_tableinits: [
        { label: '开始时间', prop: 'start_at' },
        { label: '交卷时间', prop: 'submit_at' },
        { label: '考试分数', prop: 'score' },
        { label: '考试结果', prop: 'is_pass' }
      ],
      operationinit: { width: '100', label: '查看试卷' },
      detail_total: 0,
      detail_pageNo: 1,
      pageNo: 1,
      total: 0,
		}
  },
  computed: {
//     targetFormat () {
//       return this.formData.target.map(item => {
//         let newItem = Object.assign({}, item)
//         newItem.num = item.people.length
//         return newItem
//       })
//     }
  exportUrlupexam() {
    return `${EHS.HOST}/api/v1/export/test-user/${this.questId}`  
  },
 queryParams () {
      this.pageNo = 1
      return { category_id: this.value_c, type: this.value, difficulty: this.value_n }
    }
  },

	created(){
    this.questId = this.$route.query.id;
		if(this.$route.query.is_sim){
			this.title='练习'
		}
    if(this.$route.query.checkmode) {
      this.checkmode = true
    }
		this.getInit()
	},
  methods: {
    pageNoChange (pageNo) {
      this.pageNo = pageNo
      this.$API.getExamAssign(this.questId, {page: this.pageNo, keyword: this.keyword_input,type:this.value})
      .then(data => {
        data.data.forEach(item => {
          item.finish_count = item.finish_count == null ? '暂无数据' : item.finish_count
          item.score = item.score == null ? '暂无数据' : item.score
          item.is_pass = item.is_pass == null ? '暂无数据' : item.is_pass == '1' ? '通过' : '未通过'
          item.last_submit = item.last_submit == null ? '暂无数据' : item.last_submit
          item.username = item.username ? item.username : '未知姓名'
        })
        this.tableData = data.data
      })
    },
    keywordSearch () { // 搜索关键字
      this.pageNo = 1
      this.$API.getExamAssign(this.questId, {page: this.pageNo, keyword: this.keyword_input,type:this.value})
      .then(data => {
        this.total = data.total
        data.data.forEach(item => {
          item.finish_count = item.finish_count == null ? '暂无数据' : item.finish_count
          item.is_pass = item.is_pass == null ? '暂无数据' : item.is_pass == '1' ? '通过' : '未通过'
          item.last_submit = item.last_submit == null ? '暂无数据' : item.last_submit
          item.username = item.username ? item.username : '未知姓名'
        })
        this.tableData = data.data
      })
    },
    dc(){

    },
    tabClick (tab) {
      this.activeName = tab.name
    },
		role_save(){
			let data = {
				type:'role',
				typename:'角色',
				name:[],
				ids:[]
			};

			let ids = []
			let name_array = [];

			this.formData.assignment.forEach(item=>{
				if(item.type == 'role'){
					name_array = item.name.split(',');
					data.ids = item.ids
				}
			})
			let del = []
			for(let wb in this.role_history){

				this.role_page_index[wb].forEach(item=>{
					if(!this.role_history[wb].filter(it =>it.id==item.id).length)
						del.push(item)
				})
				this.role_history[wb].forEach(item=>{
					if(!item._select){
					data.ids.push(item.id);
					name_array.push(item.name);
					}
				})

			}

			del.forEach(item=>{
				for(let da=0;da<data.ids.length;da++){
					if(item.id == data.ids[da]){
						data.ids.splice(da,1);
						name_array.splice(da,1)
					}
				}
			})
			data.name = name_array.join(',')

			console.log(data)

			if(this.formData.assignment.filter(item=>item.type == 'role').length){
				for(let a=0 ; a< this.formData.assignment.length ; a++){
					if(this.formData.assignment[a].type == 'role'){
						this.$set(this.formData.assignment,a,data)
				}
			}
			}else{
				this.formData.assignment.push(data)
			}
			this.role_show = false
		},
		role_select(row){
			this.role_history['page'+this.role_data.page] = row
		},
		role_page(index,npss=false){
			let _this = this;
			this.role_show = true;
			if(npss){
				this.role_history = {};
				this.role_page_index={};
			}
			this.$API.getLabellist({page:index,type:'user'})
			.then(data => {
				this.role_data.total = data.total
				this.role_data.page=index;
						_this.formData.assignment.forEach(item=>{
							if(item.type == 'role'){
								item.ids.forEach(id =>{
									data.data.forEach(ids =>{
										if(ids.id == id){
											ids._select = true;
										}
									})
								})
							}
						})
				if(this.role_history['page'+index]){
					this.role_history['page'+index].forEach(item=>{
							data.data.forEach(ids =>{
								if(ids.id == item.id){
									ids._select = true
								}
							})
					})
				}
				this.role_data.data = data.data;
					this.$nextTick(()=>{
						let arr = [];
						data.data.forEach(item=>{
							if(item._select){
								this.$refs.role_from.toggleRowSelection(item,true)
								arr.push(item)
							}
						})
						this.role_page_index['page'+index] = arr
					})
			})
		},
		querySearch(query,cd){
			let result = query?this.nameOptions.filter(item=>item.name.indexOf(query) !== -1):this.nameOptions;
			cd(result)
		},
		clear_user_select(){
			//清除user选择
			this.user_page_data={};
			this.user_active_list={};
			this.formData.assignment.forEach(item=>{
				if(item.type=='user'){
					item.userids=[];
					item.name=''
				}
			})
			this.getuserdata({page:this.userdata.page})
		},
		orga_active(data){
			//选择组织
			data._select =true;
			data.typename='组织机构';
			data.type='department';
			this.formData.assignment.push(data)
			let fo = JSON.parse(JSON.stringify(this.organization_form))
			this.$set(this,'organization_form',fo)

		},
		treefilter(value,data){
			if(!value)return true;
			return data.name.indexOf(value) !== -1
		},
		getInit(){

			this.$API.getTestName().then(res=>{
				this.nameOptions = res
			})
			this.$API.getTestCall().then(res=>{
				this.testOptions = res
			})
			if(this.$route.query.id){
  			this.$API.editNewTest(this.$route.query.id).then(res=>{
  				res.assignment = res.assignment?res.assignment:[];
  				res.date=[];
  				res.date.push(res.startTime);
  				res.date.push(res.endTime)
  				this.formData = res;
  			})
        if (this.checkmode) {
          this.$API.getExamAssign(this.questId, {page: 1, keyword: this.keyword_input,type:this.value})
          .then(data => {
            this.total = data.total
            data.data.forEach(item => {
              item.finish_count = item.finish_count == null ? '暂无数据' : item.finish_count
               item.score = item.score == null ? '暂无数据' : item.score
              item.is_pass = item.is_pass == null ? '暂无数据' : item.is_pass == '1' ? '通过' : '未通过'
              item.last_submit = item.last_submit == null ? '暂无数据' : item.last_submit
              item.username = item.username ? item.username : '未知姓名'
            })
            this.tableData = data.data
          })
        }
			}

		},
    save (submit) {
			this.$refs.addform.validate(valid=>{
				if(valid){
					let formData = this.formData;
					formData.action=submit;
					formData.startTime = formData.date[0];
					formData.endTime = formData.date[1];
					if(this.$route.query.is_sim){
						formData.is_sim=1
					}else{
						formData.is_sim=0
					};
					if(this.$route.query.id){
						this.$API.editSaveTest(this.$route.query.id,formData).then(res=>{
							this.$message.success('编辑成功');
							this.$router.back()
						})
					}else{
						this.$API.postNewTest(formData).then(res=>{
							this.$message.success('保存成功');
							this.$router.back()
						}).catch(res=>{})
					}
				}
			})
    },

    addVisitor () {
      let isRepeat = false
      this.formData.assignment.forEach(item => {
        if (item.type == 'visitor') {
          isRepeat = true
        }
      })
      if (isRepeat) {
        this.$message.info(`已添加访客，请勿重复添加`)
      } else {
        this.formData.assignment = [{type: 'visitor', typename: '访客', name: '访客'}].concat(this.formData.assignment)
      }
    },

    addVendor () {
      let isRepeat = false
      this.formData.assignment.forEach(item => {
        if (item.type == 'vendor') {
          isRepeat = true
        }
      })
      if (isRepeat) {
        this.$message.info(`已添加承包商，请勿重复添加`)
      } else {
        this.formData.assignment = [{type: 'vendor', typename: '承包商', name: '承包商'}].concat(this.formData.assignment)
      }
    },

    addGroup () {
			//添加机构
			this.organization = true
			this.$API.getManagement().then(res=>{
				res.forEach(item=>{

					this.formData.assignment.forEach(us=>{
						if(us.type=='department'&&item.id == us.id)item._select = true
					})
				});
				let fa = (da)=>{
						let children  = [];
						res.forEach(item=>{
							if(item.parent_id == da.id)children.push(item);
						})
						if(children.length){
							 da.children = children
						}
						return da
				}
				let data = [];
				res.forEach(item =>{
					if(item.parent_id === 0){
						data.push(fa(item))
					}
				})
				this.organization_form=data
			})
    },
    addRole () {
      this.$message.info({
        message: `添加工作角色按下`
      })
    },
    addPerson (page=1,pass=false) {
//       添加个人
	if(pass){
			this.user_page_data={};
		this.user_active_list={};
	}
		this.user_show=true;
		this.userdata.page=page;
		this.getuserdata({page})
    },
		getuserdata(params){
			// 获取个人数据
			this.$API.getUserList(params).then(res=>{
			res.data.forEach(item=>{
					this.formData.assignment.forEach(us=>{
						if(us.type=='user'){
							us.userids.forEach(it=>{
								if(it == item.id ) item._select = true
							})
						}
					})
			});

				if(this.user_page_data['page'+this.userdata.page]){
					this.user_page_data['page'+this.userdata.page].forEach(item=>{
						res.data.forEach(it=>{
						if(it.id == item.id)it._select=true
					})
					})
					}
				this.userdata.data = res.data;
				this.userdata.total=res.total;
				let arr = []
				this.$nextTick(()=>{
					res.data.map(item=>{
						if(item._select){
							this.$refs.usertable.toggleRowSelection(item);
							arr.push(item)
						}
					})
						this.user_active_list['page'+this.userdata.page] = arr

				})

			})
		},
		user_active(row){
			//选择个人
			this.user_page_data['page'+this.userdata.page]=row;

		},
		submit_user(){
			//添加个人提交
			let data = {
				type:'user',
				typename:'个人',
				num:0,
				userids:[],
				name:''
			};

			let ids = []
			let name_array = [];

			this.formData.assignment.forEach(item=>{
				if(item.type == 'user'){
					name_array = item.name.split(',');
					data.userids = item.userids
				}
			})
			if(!name_array[name_array.length-1]){
				name_array.splice(-1,1)
			};

			let del = []
			for(let wb in this.user_page_data){

				this.user_active_list[wb].forEach(item=>{
					if(!this.user_page_data[wb].filter(it =>it.id==item.id).length)
						del.push(item)
				})
				this.user_page_data[wb].forEach(item=>{
					if(!item._select){
					data.userids.push(item.id);
					name_array.push(item.username);
					}
				})

			}

			del.forEach(item=>{
				for(let da=0;da<data.userids.length;da++){
					if(item.id == data.userids[da]){
						data.userids.splice(da,1);
						name_array.splice(da,1)
					}
				}
			})
			data.name = name_array.join(',')
			data.num = data.userids.length

			//防止重复添加
			if(this.formData.assignment.filter(item=>item.type=='user').length){
				for(let a = 0 ; a < this.formData.assignment.length ; a++){
					if(this.formData.assignment[a].type=='user')this.$set(this.formData.assignment,a,data)
				}
			}else{
				this.formData.assignment.push(data)
			}
			this.user_show=false
		},
		user_soso(){
		//人员搜索
			let params = {keyword:this.user_search};
			this.getuserdata(params)
		},
    filter (scope) {
      this.$message.info({
        message: `筛选按钮按下`
      })
    },
    del (scope) {
			this.$confirm('确认要删除该项吗？','提示').then(res=>{
				this.formData.assignment.splice(scope.$index,1)
			}).catch(er=>{})
//       this.$message.info({
//         message: `删除按钮按下`
//       })
    },

    detail_pageNoChange (pageNo) {
      this.detail_pageNo = pageNo
      this.$API.getMyExamDetail(this.examid, {page: this.pageNo, userId: this.userId})
      .then(data => {
        data.data.forEach(item => {
          item.is_pass = item.is_pass ? '通过' : '未通过'
        })
        this.detail_tableData = data.data
      })
    },
    check (scope) {

      console.log(scope)
      this.userId = scope.row.id
      this.$API.getMyExamDetail(this.questId, {page: 1, userId: this.userId})
      .then(data => {
        this.dialogVisible = true
        console.log(data)
        data.data.forEach(item => {
          item.is_pass = item.is_pass ? '通过' : '未通过'
        })
        this.detail_tableData = data.data
        this.detail_total = data.total
        this.detail_pageNo = 1
      })
    },
    toError (scope) {
      let open = window.open(`/#/examing/checkError/${scope.row.id}`, '_blank')
      open.opener = null;
    }
  },
	watch:{
		organizationf_filter(val){
			this.$refs.organization.filter(val)
		},
    queryParams (curr, old) {
      this.history = {};

      this.$API.getExamAssign(this.questId, {page: 1, keyword: this.keyword_input,type:this.value})
          .then(data => {
            this.total = data.total
            data.data.forEach(item => {
              item.finish_count = item.finish_count == null ? '暂无数据' : item.finish_count
               item.score = item.score == null ? '暂无数据' : item.score
              item.is_pass = item.is_pass == null ? '暂无数据' : item.is_pass == '1' ? '通过' : '未通过'
              item.last_submit = item.last_submit == null ? '暂无数据' : item.last_submit
              item.username = item.username ? item.username : '未知姓名'
            })
            this.tableData = data.data
          })
    },
    history (curr) {
      console.log(curr)
      let arr = []
      for (const propname in curr) {
        arr = arr.concat(curr[propname])
      }
      this._mixSelectionChange(arr)
    }

	}
}
</script>

<style>
  .examAddForm-box {
    text-align: left;
  }
  .examAddForm-box .el-input.is-disabled .el-input__inner,
  .examAddForm-box .el-range-editor.is-disabled,
  .examAddForm-box .el-textarea.is-disabled .el-textarea__inner {
    background-color: #fff;
    border-color: #dcdfe6;
    color: #606266;
  }
  .examAddForm-box .times-input {
    width: 18%;
  }
  .examAddForm-box .my-textarea {
    width: 43%;
  }
  .examAddForm-box .score-input {
    width: 40%;
    min-width: 60px;
  }
  .examAddForm-box .font-blue {
    color: #0366bb;
  }
  .examAddForm-box .font-red {
    color: #F56C6C;
  }
  .my-btn {
    cursor: pointer;
  }
	.custom-tree-node{
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}
	.daocu{text-decoration: none !important;color: #606266 !important;}
</style>
